package gamagora.game
{
	import Box2D.Common.Math.b2Vec2;
	import Box2D.Dynamics.Joints.b2Joint;
	import Box2D.Dynamics.Joints.b2RevoluteJointDef;
	import Box2D.Dynamics.b2Body;
	
	import gamagora.box2d.OSPhysicBox;
	import gamagora.box2d.OSPhysicBridge;
	import gamagora.box2d.OSPhysicData;
	import gamagora.box2d.OSPhysicWorld;
	import gamagora.game.utils.OSImages;
	
	import org.flixel.FlxGroup;
	import org.flixel.FlxSprite;

	public class OSBoxBridge extends OSPhysicBridge
	{
		private var _plankWidth:Number;
		private var _plankHeight:Number;
		private var _planks:Vector.<OSPhysicBox>;
		private var _anchor:b2Vec2;
		private var _jd:b2RevoluteJointDef;
		
		public function OSBoxBridge(X:Number, Y:Number, plankWidth:Number, plankHeight:Number, nbPlanks:uint = 10)
		{
			super(X, Y, nbPlanks);
			_plankWidth = plankWidth;
			_plankHeight = plankHeight;
			_planks = new Vector.<OSPhysicBox>();
	
			_anchor = new b2Vec2();
			
			_jd = new b2RevoluteJointDef();
			_jd.lowerAngle = -15 / (180 / Math.PI);
			_jd.upperAngle = 15 / (180 / Math.PI);
			_jd.enableLimit = true;
			
			var halfWidth:Number = _plankWidth / 2;
			
			for (var i:uint = 0; i < _nbPlanks; ++i)
			{
				var plank:OSPhysicBox = new OSPhysicBox((x + halfWidth + _plankWidth * i), y, _plankWidth, _plankHeight);
				plank.setData(OSPhysicData.GROUND);
				plank.getPhysic().bodyDef.type = b2Body.b2_dynamicBody;
				
				plank.getPhysic().fixDef.friction = 0.2;
				plank.getPhysic().fixDef.density = 1.0;
				plank.makeGraphic(_plankWidth, _plankHeight, 0xff000000);

				_planks.push(plank);
			}
		}
		
		public override function create(group:FlxGroup):void
		{
			super.create(group);
			
			var ground:b2Body = OSPhysicWorld.getInstance().GetGroundBody();
			var prevBody:b2Body = ground;
			
			for (var i:uint = 0; i < _nbPlanks; ++i)
			{			
				var plank:OSPhysicBox = _planks[i];
				plank.resetPosition();
				plank.create(group);
				_anchor.Set((x + _plankWidth * i) / OSPhysicWorld.ratio, y / OSPhysicWorld.ratio);
				
				_jd.Initialize(prevBody, plank.getBody(), _anchor);
				OSPhysicWorld.getInstance().CreateJoint(_jd);
				
				prevBody = plank.getBody();
			}
			
			_anchor.Set((x + _plankWidth * _nbPlanks) / OSPhysicWorld.ratio, y / OSPhysicWorld.ratio);
			_jd.Initialize(prevBody, ground, _anchor);
			OSPhysicWorld.getInstance().CreateJoint(_jd);
		}
		
		public override function update():void
		{
			super.update();
			for(var i:uint=0;i<_nbPlanks;++i)
			{
				_planks[i].update();
			}
		}
		
		public override function destroyPhysic():void
		{
			super.destroyPhysic();
			for(var i:uint=0; i<_nbPlanks; i++)
			{
				_planks[i].destroyPhysic();
			}
		}
		
		public override function destroy():void
		{
			super.destroy();
			for(var i:uint=0;i<_nbPlanks;i++)
			{
				_planks[i].destroy();
			}
		}
		
		public override function draw():void
		{
			//super.draw();
			for(var i:uint=0;i<_nbPlanks;i++)
			{
				_planks[i].draw();
			}
		}
		
		public override function kill():void
		{
			super.kill();
			for(var i:uint=0;i<_nbPlanks;i++)
			{
				_planks[i].kill();
			}		
		}
		
		public override function reset(x:Number, y:Number):void
		{
			super.reset(x, y);
			for(var i:uint=0;i<_nbPlanks;i++)
			{
				_planks[i].reset(x, y);
			}				
		}
	}
}
